import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_demo/res/resources.dart';
import 'package:flutter_demo/util/theme_utils.dart';

class PostionAppBar extends StatelessWidget implements PreferredSizeWidget {
  const PostionAppBar({
    Key key,
    this.backgroundColor,
    this.leftButton,
    this.leftText,
    this.title,
    this.actions,
    this.onLeftButton,
  }) : super(key: key);

  final IconButton leftButton;
  final String leftText;
  final String title;
  final List<Widget> actions;
  final Color backgroundColor;
  final Function onLeftButton;

  @override
  Widget build(BuildContext context) {

    Color _backgroundColor;
    if (_backgroundColor == null){
      _backgroundColor = ThemeUtils.getBackgroundColor(context);
    }else{
      _backgroundColor = backgroundColor;
    }
    //黑色主题切换
    SystemUiOverlayStyle _overlayStyle = ThemeData.estimateBrightnessForColor(_backgroundColor) != Brightness.dark
    ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark;

    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: _overlayStyle,
      child: Material(
        color:backgroundColor,
        child: Container(
          decoration: new BoxDecoration(
            // color: backgroundColor,
            gradient: LinearGradient(colors: const [Color(0xFF5793FA), Color(0xFF4647FA)])
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              leftButton!=null?leftButton:IconButton(icon: Icon(Icons.location_on),color:ThemeUtils.isDark(context)? Colours.dark_text:Colors.white,onPressed: onLeftButton,),
              Text(leftText!=null?leftText:'未知',style: TextStyle(color:ThemeUtils.isDark(context)? Colours.dark_text:Colors.white,),),
              Expanded(
                flex:3,
                child: Center(
                  child: Text(title!=null?title:'',style:TextStyle(fontSize:Dimens.font_sp18,color:ThemeUtils.isDark(context)? Colours.dark_text:Colors.white,)),
                ),
              ),
              ...actions.map((wid){
                return Expanded(
                  child: wid,
                );
              }).toList(),
            ],
          ),
          alignment: Alignment.centerLeft,
          padding: EdgeInsets.only(top:25.0),
        ),
      ),
    );
  }

  @override
  // TODO: implement preferredSize
  Size get preferredSize => Size.fromHeight(48.0);
}